﻿using BCCommon;
using BCData.OA.WorkflowTemplate.WorkflowTemplateBusiness;
using BCDto.OA.WorkflowTemplate.WorkflowTemplate;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateBusiness;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.OA.WorkflowTemplate.WorkflowTemplateBusiness
{
    public class WorkflowTemplateBusinessService : IWorkflowTemplateBusinessService
    {
        private readonly IRedisService redisService;
        private readonly IWorkflowTemplateBusinessData workflowTemplateBusinessData;
        private readonly string key = string.Format("{0}", EntityNameConst.WorkflowTemplateBusinessEntity);
        public WorkflowTemplateBusinessService(IRedisService redisService,
            IWorkflowTemplateBusinessData workflowTemplateBusinessData)
        {
            this.redisService = redisService;
            this.workflowTemplateBusinessData = workflowTemplateBusinessData;
        }

        public IEnumerable<WorkflowTemplateBusinessDto> GetAllowInitiateList(int companyId, int departmentId, int positionId, long adminId,string categoryCode)
        {
            string cacheKey = this.redisService.GetKey(string.Format("{0}GetAllowInitiateList", key), companyId, departmentId, positionId, adminId, categoryCode);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.workflowTemplateBusinessData.GetAllowInitiateList(companyId, departmentId, positionId, adminId, categoryCode).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            if (result == null)
            {
                this.redisService.DeleteAsync(cacheKey);
            }
            return result.As<IEnumerable<WorkflowTemplateBusinessDto>>();
        }
    }
}
